<?php
	function getClient($id)
	{
		if($id > 0)
		{
			$result = mysql_query('SELECT * FROM '.DB_PERFIX.'admin_clients WHERE id='.$id.';');
			while($row = mysql_fetch_array($result))
			{
				$item['id'] = $row['id'];
				$item['name'] = $row['name'];
				$item['contact'] = $row['contact'];
				$item['email'] = $row['email'];
				$item['msn'] = $row['msn'];
				$item['skype'] = $row['skype'];
				$item['description'] = $row['description'];
			}
			return $item;
		}
	}
	
	function addSiteCore($site, $core)
	{	
		if(($site > 0) && ($core > 0))
		{
			$insert  = 'INSERT INTO '.DB_PERFIX.'site_cores (`site_id`, `core_id`, `active`) '; 
			$insert .= 'VALUES ('.$site.', '.$core.', 1);';
			$result = mysql_query($insert);
		}
	}
	
	function setSiteCoreActive($site, $core, $active=1)
	{	
		if(($site > 0) && ($core > 0))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'site_cores SET active='.$active.' WHERE site_id='.$site.' AND core_id='.$core.';'; 
			$result = mysql_query($sql);
		}
	}
	
	function getSiteCores()
	{
		$result = mysql_query('SELECT id, site_id, core_id, active FROM '.DB_PERFIX.'site_cores;');
		while($row = mysql_fetch_array($result))
		{
			$item[$row['site_id']][$row['core_id']] = $row['active'];
		}
		return $item;
	}
	
	function getAllCores()
	{
		$result = mysql_query('SELECT id, title FROM '.DB_PERFIX.'katana_cores;');
		while($row = mysql_fetch_array($result))
		{
			$item[$row['id']]['id'] = $row['id'];
			$item[$row['id']]['title'] = $row['title'];
		}
		return $item;
	}
	
	function addClient($name)
	{	
		$name = trim($name);	
		$insert  = "INSERT INTO ".DB_PERFIX."admin_clients (`id`, `name`)"; 
		$insert .= "VALUES (NULL, '".$name."');";
		$result = mysql_query($insert);
	}

	function updateClient($id, $name, $contact, $email, $msn, $skype='', $description='')
	{
		$name  	 = trim($name); 
		$contact = trim($contact);
		if($name != '')
		{
			$update  = ' UPDATE '.DB_PERFIX.'admin_clients SET ';
			$update .= ' `name` = "'.$name.'", ';
			$update .= ' `contact` = "'.$contact.'", ';
			$update .= ' `email` = "'.$email.'", ';
			if($skype != '')
			{ $update .= ' `skype` = "'.$skype.'", '; }
			if($skype != '')
			{ $update .= ' `description` = "'.$description.'", '; }
			$update .= ' `msn` = "'.$msn.'" ';
			$update .= ' WHERE id='.$id.' LIMIT 1; ';
			$result = mysql_query($update);
		}
	}
	
	function getSiteTable($id)
	{
		$select = 'id, client_id, title, site_url, license, language, meta_data, meta_key, analytics_id, active';
		$result = mysql_query('SELECT '.$select.' FROM '.DB_PERFIX.'sites WHERE id='.$id.';');
		while($row = mysql_fetch_array($result))
		{
			$item['id'] 			= $row['id'];
			$item['client_id'] 		= $row['client_id'];
			$item['title'] 			= $row['title'];
			$item['site_url'] 		= $row['site_url'];
			$item['license'] 		= $row['license'];
			$item['language']		= $row['language'];
			$item['meta_data'] 		= $row['meta_data'];
			$item['meta_key'] 		= $row['meta_key'];
			$item['analytics_id'] 	= $row['analytics_id'];
			$item['active'] 		= $row['active'];
		}
		return $item;
	}
	
	function addSite($title, $client_id)
	{	
		$client = getClient($client_id);
		if($client['id'] > 0)
		{
			$title = trim($title);	
			$insert  = "INSERT INTO ".DB_PERFIX."sites (`id`, `client_id`, `title`)"; 
			$insert .= "VALUES (NULL, ".$client['id'].", '".$title."');";
			$result = mysql_query($insert);
		}
	}
	
	function updateSite($id, $title, $site_url, $license, $language)
	{
		if($license != md5(md5($language).md5($id).md5($site_url)))
		{ $license = md5(md5($language).md5($id).md5($site_url)); }
		$title 	= stripNonAlphaNum($title); 
		$update  = ' UPDATE '.DB_PERFIX.'sites SET ';
		$update .= ' `id` = '.$id.', ';
		$update .= ' `title` = "'.$title.'", ';
		$update .= ' `site_url` = "'.$site_url.'", ';
		$update .= ' `license` = "'.$license.'", ';
		$update .= ' `language` = "'.$language.'" ';
		$update .= '  WHERE id='.$id.' LIMIT 1; ';
		$result = mysql_query($update);
	}
?>